package com.zlzxm.studentscreen.util.applog;

import android.annotation.SuppressLint;
import android.util.Log;

import com.zlzxm.studentscreen.util.FileUtil;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;


/**
 * CREATE BY zlzxm ON 11/25/20
 *
 * 生成各种日志文件数据
 */
public class Logger {

    private static final String TAG = "Logger";

    @SuppressLint("ConstantLocale")
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
    @SuppressLint("ConstantLocale")
    private static final SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
    private static final String  extensions = ".txt";
    private  static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private Logger() {
    }




    public static void d(String msg,String path){

        File file =  FileUtil.createFile(path + dateFormat.format(new Date())+extensions);

        FileOutputStream fileOutputStream = null;
        try {

            StringBuilder errorText = new StringBuilder();

            errorText.append(msg);

            RandomAccessFile accessFile = new RandomAccessFile(file,"rw");

            long length =  accessFile.length();

            accessFile.seek(length);

            accessFile.write(errorText.toString().getBytes());

            accessFile.close();

//            fileOutputStream = new FileOutputStream(file,true);
//
//            StringBuilder errorText = new StringBuilder();
//
//            errorText.append(msg);
//
//            fileOutputStream.write(errorText.toString().getBytes());
//
//            fileOutputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }


    //打印出程序异常信息到本地
    public static void e(Throwable e,String path){

        File file =  FileUtil.createFile(path + dateFormat.format(new Date())+extensions);

        if(file != null){

            try {

//                FileOutputStream fileOutputStream = new FileOutputStream(file,true);

                RandomAccessFile accessFile = new RandomAccessFile(file,"rw");

                long length =  accessFile.length();

                accessFile.seek(length);

                StringBuilder errorText = new StringBuilder();

                errorText.append("\r\n")
                        .append(timeFormat.format(new Date()))
                        .append("(设备时间)")
                        .append("\r\n");


                StringWriter mStringWriter = new StringWriter();
                PrintWriter mPrintWriter = new PrintWriter(mStringWriter);
                e.printStackTrace(mPrintWriter);
                mPrintWriter.close();

                Log.e(TAG, mStringWriter.toString());

                errorText.append(mStringWriter.toString());

                accessFile.write(errorText.toString().getBytes());

                accessFile.close();

            } catch (FileNotFoundException fileNotFoundException) {
                fileNotFoundException.printStackTrace();
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }


        }
    }


}
